<?php
class HyperlinkDao
{
	private $hlink;
	private $dbh;
	public function __construct()
	{
		$db = LtObjectUtil::singleton('LtDb');
		$db->group = "spreaddb";
		$db->node = "node_0";
		$db->init();
		$this->hlink = $db->getTDG("hyperlinks");
		$this->dbh = $db->getDbHandle();
	}

	public function getByid($cid)
	{
		return $this->hlink->fetch($cid);
	}

	public function getCount($where = '', $whereVs=array())
	{
		$conds=array();
		if(!empty($where))
		{
			$conds['where']=array();
			$conds['where']['expression'] = $where;

			if(!empty($whereVs))
			{
				$conds['where']['value'] = $whereVs;	
			}
		}

		return $this->hlink->count($conds);
	}

	public function getList($where = '', $whereVs=array(), $orderby='', $offset=0, $limit=20)
	{
		$conds=array();
		if(!empty($where))
		{
			$conds['where']=array();
			$conds['where']['expression'] = $where;

			if(!empty($whereVs))
			{
				$conds['where']['value'] = $whereVs;	
			}
		}

		if(!empty($orderby))
		{
			$conds['orderby'] = $orderby;
		}

		$conds['offset'] = $offset;
		$conds['limit'] = $limit;
		
		return $this->hlink->fetchRows($conds);
	}

	public function add($data)
	{ 
		$result = $this->hlink->insert($data);
		return $result;
	}

	public function delByid($linkid)
	{
		return $this->hlink->delete($linkid);	
	}

	public function editByid($linkid, $updata)
	{
		if(empty($linkid) || empty($updata))
		{
			return false;
		}
		$conds=array('expression'=>'linkid=:linkid', 'value'=>array('linkid'=>$linkid));
		return $this->hlink->updateRows($conds, $updata);	
	}
}
